#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   model_utils.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/14 0014 13:49   st      1.0         None
'''
import pickle
from utils.constent import *


def get_stopwords():
    """
    获取停用词
    :return:
    """
    with open(stop_word_path, 'r', encoding='utf-8') as f:
        stopword = [line.strip() for line in f]
    return set(stopword)


def create_ngram(input_list, start=1, end=3):
    """
    生成ngram
    """
    result = []
    for i in range(start, end+1):
        result.extend(zip(*[input_list[j:] for j in range(i)]))
    return result


def create_ngram_1(input_list, ngram_num=1):
    """
    生成ngram
    """
    result = []
    for i in range(ngram_num, ngram_num+1):
        result.extend(zip(*[input_list[j:] for j in range(i)]))
    return result


def save_model(model, filename):
    """
    保存模型
    :param model: 模型数据
    :param filename: 保存文件名
    :return:
    """
    if os.path.exists(filename):
        os.remove(filename)
    with open(filename, 'wb') as fw:
        pickle.dump(model, fw)


def load_model(filename):
    """
    加载模型
    :param filename: 文件路径
    :return:
    """
    with open(filename, 'rb') as fr:
        model = pickle.load(fr)
    return model


if __name__ == '__main__':
    print(create_ngram(['1', '2', '3', '4', '5', '6', '7'], 2, 6))